package com.syzygy.controller;

import com.alibaba.fastjson2.JSONArray;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.HttpResponse;
import com.syzygy.pojo.Picture;
import com.syzygy.pojo.Type;
import com.syzygy.pojo.User;
import com.syzygy.service.PictureService;
import com.syzygy.service.TypeService;
import com.syzygy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import util.PictureUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/")
public class IndexController {
    @Autowired
    PictureService pictureService;
    @Autowired
    UserService userService;
    @Autowired
    TypeService typeService;
    @Autowired
    PictureUtils pictureUtils;

    @RequestMapping("/")   //主页
    protected String indexPage() {
        return "redirect:/Page/1";
    }

    @RequestMapping(value = "/Page/{pageNumber}", method = RequestMethod.GET)   //主页
    protected String indexPagee(Model model,
                                HttpSession session,
                                @PathVariable Integer pageNumber) {
//      带分页功能  照片卡片的加载
        PageInfo<Picture> page = pictureService.getPicturePage(pageNumber);
        String pageJSON = JSONArray.toJSONString(page);
        model.addAttribute("page", page);
        model.addAttribute("pageJSON", pageJSON);

//        类型导航的加载
        List<Type> allType = typeService.getAllType();
        System.out.println(allType);
        model.addAttribute("allType", allType);
        String allTypeJSON = JSONArray.toJSONString(allType);
        model.addAttribute("allTypeJSON", allTypeJSON);

//        个人头像的加载
        Object userIdSession = session.getAttribute("userId");
        if (null != userIdSession) {
            int userId = Integer.parseInt(userIdSession.toString());
            User user = userService.getUserByUserId(userId);
            model.addAttribute("user", user);
        }

        return "index2";
    }


    //    -----------------------------------------------------------------------------------------------
    @RequestMapping("/test1")
    protected String test1() {
        return "footer";
    }

}
